<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Crud extends CI_Controller {
    
    public $total_tables = array();
    
    function __construct() {
        parent::__construct();
        $this->load->language('crud_adm_' . $this->config->item('language'), $this->config->item('language'));
        $this->config->load('database_tables');
        $this->total_tables = $this->config->item('dbtables');
    }

    public function index() {
        if (!$this->ion_auth->logged_in()) {
            redirect('crud/login');
        } else {
            $data['title'] = TITLES . lang('inicio');
            $data['grafico_visitas'] = $this->get_grafico();
            $this->load->view('index', $data);
        }
    }
    
    public function get_grafico($mes = null, $ano = null){
        $mes || $mes = $this->input->post('mes');
        $ano || $ano = $this->input->post('ano');
        
        $this->load->language('calendar_' . $this->config->item('language'), $this->config->item('language'));
        $this->load->library('Grafico');
        if($mes == null){$mes = date('m');}
        if($ano == null){$ano = date('Y');}
        
        $data['mes'] = $mes;
        $data['ano'] = $ano;
        
        $data['array_visitas'] = $this->grafico->get_array_visitas($mes, $ano);
        if($this->input->post('output') == 'echo'){
            echo $this->load->view('grafico_visitas', $data, true);
        }else{
            return $this->load->view('grafico_visitas', $data, true);
        }
    }

    public function login() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('p_identity', lang('email'), 'required');
        $this->form_validation->set_rules('p_password', lang('contrasena'), 'required');
        $this->data['title'] = TITLES . lang('login');
        if ($this->form_validation->run() == true) { //check to see if the user is logging in
            //check for "remember me"
            $remember = (bool) $this->input->post('p_remember');

            if ($this->ion_auth->login($this->input->post('p_identity'), $this->input->post('p_password'), $remember)) { //if the login is successful
                //redirect them back to the home page
                $this->session->set_flashdata('message', $this->ion_auth->messages());
                redirect('crud');
            } else { //if the login was un-successful
                //redirect them back to the login page
                $this->data['message'] = $this->ion_auth->errors();
                $this->load->view('login', $this->data);
            }
        } else {  //the user is not logging in so display the login page
            //set the flash data error message if there is one
            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
            $this->load->view('login', $this->data);
            //$this->load->view('auth/login', $this->data);
        }
    }

    public function registrar() {
        $this->load->library('captcha/recaptcha');
        $this->data['title'] = TITLES . lang('registrar');
        //validate form input
        $this->load->library('form_validation');
        $this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
        $this->form_validation->set_rules('username', 'UserName', 'required|xss_clean');
        //$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
        $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('idioma', 'Idioma', 'required');
			
        if ($this->form_validation->run() == true) {
            if(!$this->recaptcha->check_answer($this->input->ip_address(), $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'))){
                $this->session->set_flashdata('message', $this->lang->line('recaptcha_incorrect_response'));
                redirect('crud/registrar');
            }

            $this->load->model('admin_model');
            $username = strtolower($this->input->post('username'));
            $email = $this->input->post('email');
            $password = $this->input->post('password');

            $additional_data = array(
                'first_name' => $this->input->post('first_name'),
                'idioma' => $this->input->post('idioma'),
                'idi_spanish' => ($this->input->post('p_idioma_esp') != '' ? 'S' : 'N'),
                'idi_english' => ($this->input->post('p_idioma_ing') != '' ? 'S' : 'N'),
                'idi_italian' => ($this->input->post('p_idioma_ita') != '' ? 'S' : 'N'),
                'idi_german' => ($this->input->post('p_idioma_ale') != '' ? 'S' : 'N'),
                'idi_french' => ($this->input->post('p_idioma_fra') != '' ? 'S' : 'N'),
                'idi_portuguese' => ($this->input->post('p_idioma_por') != '' ? 'S' : 'N'),
                'idi_norwegian' => ($this->input->post('p_idioma_nor') != '' ? 'S' : 'N'),
                'idi_swedish' => ($this->input->post('p_idioma_sue') != '' ? 'S' : 'N'),
                'company' => $this->admin_model->insert_empresa()
            );
        }
        if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data)) { //check to see if we are creating the user
            //redirect them back to the admin page
            $this->session->set_flashdata('message', lang('usuario_creado'));
            redirect('crud/login');
        } else { //display the create user form
            //set the flash data error message if there is one
            $this->load->helper('directory');
            $this->load->language('usuario_' . $this->config->item('language'), $this->config->item('language'));
            $this->data['var_post'] = $_POST;
            $this->data['idiomas'] = directory_map('./application/language/', 1);
            $this->data['recaptcha'] = $this->recaptcha->get_html();
            $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
            $this->load->view('registrar', $this->data);
        }
    }

    //log the user out
    function logout() {
        $this->data['title'] = "Logout";
        //log the user out
        $logout = $this->ion_auth->logout();
        //redirect them back to the page they came from
        redirect('crud', 'refresh');
    }
    
    function perfil() {
        $this->load->helper('directory');
        $this->load->library('settings_crud');
        $this->load->helper('generamtos');
        $this->load->language('usuario_' . $this->config->item('language'), $this->config->item('language'));
        $data['fields'] = $this->settings_crud->get_columns_form($this->total_tables['users']);
        $data['redireccion_retorno'] = 'perfil';
        $data['output'] = $this->load->view('mantenimientos/perfil', $data, 'true');
        $data['title'] = TITLES.lang('title_wizard_producto');
        $this->load->view('plantillas/contenedor_wizard', $data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */